package com.zhang.family_doctor.entity;

import com.zhang.family_doctor.enums.BusinessCodeEnum;
import com.zhang.family_doctor.enums.ResponseConstantEnum;
import lombok.Data;

/**
 * TODO
 * R类封装返回工具类
 *
 * @author ygl
 * @version V1.0
 * @since 2021-07-20 17:16
 **/
@Data
public class R<T> {

    private static final long serialVersionUID = 1L;

    //响应码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;


    /**
     * 操作成功ok方法
     */
    public static <T> R<T> success(T data) {

        R<T> response = new R();
        response.setCode(ResponseConstantEnum.SUCCESS.getResultCode());
        response.setMessage(ResponseConstantEnum.SUCCESS.getResultMessage());
        response.setData(data);
        return response;
    }

    /**
     * 操作成功success方法
     */
    public static <T> R<T> success(String success) {

        R<T> response = new R();
        response.setCode(ResponseConstantEnum.SUCCESS.getResultCode());
        response.setMessage(success);
        return response;
    }


    /**
     * 编译失败方法
     */
    public static R buildFailure(Integer errCode, String errMessage) {

        R response = new R();
        response.setCode(errCode);
        response.setMessage(errMessage);
        return response;
    }

    /**
     * 编译失败方法
     */
    public static R buildFailure( String errMessage) {

        R response = new R();
        response.setCode(BusinessCodeEnum.UNKNOWN_REASON.getCode());
        response.setMessage(errMessage);
        return response;
    }
}
